﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace A1
{
    class ControlMenu
    {
        private SpriteFont font; //The game font
        private bool fourPlayer; //Whether we have four players
        private Texture2D spriteSheet; //The sprites
        private int GameHeight; //The game height
        private int GameWidth;//The game width
        private Ball ball; //The ball
        private Game1.GameState nextState; //The next state to enter
        private InputManager inputs; //The input manager
        private string[] messages; //The messages to display
        private List<Paddle> paddles; //The list of paddles
        private Game1.Difficulty difficulty; //The difficulty of the game
        private GameTime gameTime; //The gametime object
        private Vector2[] labelPositions; //The positions of the "LEFT, Right..." etc positions
        private Vector2 instructionPos; //The positions of the player select instructions
        private Vector2 descriptionPos; //The position of the game description

        /// <summary>
        /// Create a new control menu
        /// </summary>
        /// <param name="fourPlayer">Whether we have four players</param>
        /// <param name="font">The game font</param>
        /// <param name="NextState">The next state (game mode)</param>
        /// <param name="inputManager">The input manager</param>
        /// <param name="paddles">The paddles</param>
        /// <param name="sprites">The sprites for the paddles</param>
        /// <param name="Height">The game height</param>
        /// <param name="Width">The game width</param>
        /// <param name="b">The ball</param>
        /// <param name="difficulty">The game difficulty</param>
        /// <param name="gameTime">The gametime object</param>
        public ControlMenu(bool fourPlayer, SpriteFont font, Game1.GameState NextState, InputManager inputManager,
            List<Paddle> paddles, Texture2D sprites, int Height, int Width, Ball b, Game1.Difficulty difficulty, GameTime gameTime)
        {
            descriptionPos = new Vector2(10, 400);
            instructionPos = new Vector2(20, 10);
            labelPositions = new Vector2[4];
            for (int i = 0; i < 4; i++)
            {
                labelPositions[i] = new Vector2(200, (i * 50) + 200);
            }
            this.paddles = paddles;
            inputs = inputManager;
            nextState = NextState;
            this.font = font;
            this.fourPlayer = fourPlayer;
            spriteSheet = sprites;
            GameHeight = Height;
            GameWidth = Width;
            this.gameTime = gameTime;
            ball = b;
            this.difficulty = difficulty;
            messages = new string[4];
            for (int i = 0; i < 4; i++)
            {
                messages[i] = "CPU";
            }
        }

        /// <summary>
        /// Update the control menu
        /// The control menu will not allow more than one person to be in a position on the field
        /// and will prevent someone from being in two positions
        /// </summary>
        /// <returns>The next game state</returns>
        public Game1.GameState Update()
        {
            if (AddKeyboardPaddle(Keys.D, Game1.Position.Right, 0))
            {
                CheckForCpu(Game1.Position.Right);
                paddles.Add(new Player(false, 0, Game1.Position.Right, spriteSheet, GameHeight, GameWidth, ball, gameTime));
                messages[0] = "Keyboard";
            }
            else if (AddKeyboardPaddle(Keys.A, Game1.Position.Left, 1))
            {
                CheckForCpu(Game1.Position.Left);
                paddles.Add(new Player(false, 0, Game1.Position.Left, spriteSheet, GameHeight, GameWidth, ball, gameTime));
                messages[1] = "Keyboard";
            }
            else if (AddKeyboardPaddle(Keys.W, Game1.Position.Top, 2))
            {
                CheckForCpu(Game1.Position.Top);
                paddles.Add(new Player(false, 0, Game1.Position.Top, spriteSheet, GameHeight, GameWidth, ball, gameTime));
                messages[2] = "Keyboard";
            }
            else if (AddKeyboardPaddle(Keys.S, Game1.Position.Bottom, 3))
            {
                CheckForCpu(Game1.Position.Bottom);
                paddles.Add(new Player(false, 0, Game1.Position.Bottom, spriteSheet, GameHeight, GameWidth, ball, gameTime));
                messages[3] = "Keyboard";
            }
            if (inputs.KeyReady(Keys.Enter))
            {
                return nextState;
            }
            for (int i = 0; i < 4; i++)
            {
                if (AddPaddle(i, Buttons.B, Game1.Position.Right, 0))
                {
                    CheckForCpu(Game1.Position.Right);
                    messages[0] = "Gamepad" + i;
                    paddles.Add(new Player(true, i, Game1.Position.Right, spriteSheet, GameHeight, GameWidth, ball, gameTime));

                }
                else if (AddPaddle(i, Buttons.X, Game1.Position.Left, 1))
                {
                    CheckForCpu(Game1.Position.Left);
                    messages[1] = "Gamepad" + i;
                    paddles.Add(new Player(true, i, Game1.Position.Left, spriteSheet, GameHeight, GameWidth, ball, gameTime));

                }
                else if (AddPaddle(i, Buttons.Y, Game1.Position.Top, 2))
                {
                    CheckForCpu(Game1.Position.Top);
                    messages[2] = "Gamepad" + i;
                    paddles.Add(new Player(true, i, Game1.Position.Top, spriteSheet, GameHeight, GameWidth, ball, gameTime));

                }
                else if (AddPaddle(i, Buttons.A, Game1.Position.Bottom, 3))
                {
                    CheckForCpu(Game1.Position.Bottom);
                    messages[3] = "Gamepad" + i;
                    paddles.Add(new Player(true, i, Game1.Position.Bottom, spriteSheet, GameHeight, GameWidth, ball, gameTime));

                }
                if (inputs.ButtonReady(Buttons.Start, i))
                {
                    return nextState;
                }
            }

            return Game1.GameState.Setup;
        }

        /// <summary>
        /// Adds a paddle to the list of paddles
        /// </summary>
        /// <param name="GamePad">The gamepad to use</param>
        /// <param name="button">The button</param>
        /// <param name="pos">The position to place the paddle at</param>
        /// <param name="message">The string the the array to change</param>
        /// <returns></returns>
        private bool AddPaddle(int GamePad, Buttons button, Game1.Position pos, int message)
        {
            if (inputs.ButtonReady(button, GamePad))
            {
                foreach (Paddle p in paddles)
                {
                    if (/*(p as Player).Index == (PlayerIndex)GamePad*/p is Player)
                    {
                        if (p.Position == pos)
                        {
                            paddles.Remove(p);
                            paddles.Add(new AI(spriteSheet, pos, ball, GameWidth, GameHeight, difficulty, gameTime));
                            messages[message] = "CPU";
                        }
                        return false;
                    }
                }
                return true;
            }
            return false;
        }

        private bool AddKeyboardPaddle(Keys key, Game1.Position pos, int message)
        {
            if (inputs.KeyReady(key))
            {
                foreach (Paddle p in paddles)
                {
                    if (/*(p as Player).UsingGamepad*/p is Player)
                    {
                        if (p.Position == pos)
                        {
                            paddles.Remove(p);
                            paddles.Add(new AI(spriteSheet, pos, ball, GameWidth, GameHeight, difficulty, gameTime));
                            messages[message] = "CPU";
                        }
                        return false;
                    }
                }
                return true;
            }
            return false;
        }
        /// <summary>
        /// Check if a cpu is in the list spot
        /// </summary>
        /// <param name="possiblePos">The position to check for a cpu</param>
        public void CheckForCpu(Game1.Position possiblePos)
        {
            for (int i = 0; i < paddles.Count; i++)
            {
                if (paddles[i].Position == possiblePos)
                {
                    paddles.Remove(paddles[i]);
                }
            }
        }
        /// <summary>
        /// Draw the control menu
        /// </summary>
        /// <param name="spriteBatch">the spritebatch object</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(font, "Press ABXY or WASD that corresponds to the position \nyou want! Press again to deselect", instructionPos, Color.White);
            spriteBatch.DrawString(font, "Right: ", labelPositions[0], Color.White);
            spriteBatch.DrawString(font, "Left: ", labelPositions[1], Color.White);
            spriteBatch.DrawString(font, "Top: ", labelPositions[2], Color.White);
            spriteBatch.DrawString(font, "Bottom: ", labelPositions[3], Color.White);
            for (int i = 0; i < 4; i++)
            {
                spriteBatch.DrawString(font, messages[i], new Vector2(300, (50 * i) + 200), Color.White);
            }
            spriteBatch.DrawString(font, "Last one standing wins! Hit power up blocks to make \nzany effect happen to the ball, or release power ups!", descriptionPos, Color.White);
        }
    }
}
